#ifndef MODEM_H_
#define MODEM_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <assert.h>
#include <string.h>
//#include <ctl.h>
//#include <ctl_api.h>
#include <nxp/iolpc2378.h>
//#include <targets/liblpc2000.h>
//#include <cross_studio_io.h>

//#include "timer.h"
#include "buffer.h"
 #include "TypeDef.h"
typedef enum {
  GSM_INACTIVE = 0,
  GSM_DISCONNECTED,
  GSM_CONNECTED, 
  GSM_ERROR
} GSM_STATUS_t;

typedef enum {
  GSM_LOST = 0,
  GSM_RESEIVE,
  GSM_NOT_FOUND_COMM
} GSM_STATUS_r;

#define IIR1 1
#define IIR2 2
#define IIR3 3
#define JSON_BUFFER_SIZE 512


  void SLEEP_MODE(void);
  void SLEEP_MODE_GSM(void);

void gsm_start_init(void);
void config_init(void);
 void SAVE_CONFIG(void);
 void READ_CONFIG(int conf_n);
extern EKdata EK_Current, EK_ArMP, EK_ArDay;
extern char ArMPread, ArDAYread;
 int GET_BUFF_RESERV(char *out_SD2,int pxc2); 
int GET_BUFF(char *out_SD,int pxc);
int GET_AIN(int CF_AIN);
int GET_DIN(int CF_DIN);
int GET_DI(int DI_NUM);
int GET_CORR1();
int GET_CORR2();
int GET_CORR1S();
int GET_CORR2S();
int GET_ENERGY();
int GET_POW();
int GET_REG_INFO();
int GET_DOUT(int DOUT_NUM);
int GET_AOUT(int AOUT_NUM);
int GET_REG(int REG_NUM);
int GET_ALARM(int i);
int OPROSS();
void CREAT_PACKET(int SDHC,int TR);
void REF_UART_D(int RFU1);
GSM_STATUS_t GSM_TRANSF_DATA_UP(int nt);
GSM_STATUS_t GSM_FIND_TR(int ERROR_GS_F);
GSM_STATUS_r GSM_FIND_DATA(int ERROR_GS_F);
GSM_STATUS_t GSM_TRANSF_DATA(int ERROR_GS);
GSM_STATUS_t GSM_TRANSF_DATA1(int ERROR_GS1);
GSM_STATUS_t GSM_TRANSF_ARCHIV(char outsdx);
int CONNECT_UART_READY();
int OPT_GSMSIGN(int options_sign);
void gsm_start_set(int start_set);
void runner_create_packet_conf(int confA,int sysx);
void SHUT_DOWN_MODEM(int sleep_m);
void CHAN_SIM(void);
void runner_read_alarm(void);
void SEARCH_PERIOD(void);
#pragma location = "EMAC_DMA_RAM"
static char out_CAM[JSON_BUFFER_SIZE]="";
#pragma location = "EMAC_DMA_RAM"
static char out_CSX[30]="";

void RESEND_ARHIV(int position);


#pragma location = "EMAC_DMA_RAM"
static char out[JSON_BUFFER_SIZE]="";
void install_gsm_irq(void);
#pragma location = "EMAC_DMA_RAM"
static char out_S1[JSON_BUFFER_SIZE]="";
#pragma location = "EMAC_DMA_RAM"
static char out_UART[JSON_BUFFER_SIZE]="";
#pragma location = "EMAC_DMA_RAM"
static char out_UART1[JSON_BUFFER_SIZE]="";
#pragma location = "EMAC_DMA_RAM"
static char out_SD[JSON_BUFFER_SIZE]="";
#pragma location = "EMAC_DMA_RAM"
static char out_SDRR[JSON_BUFFER_SIZE]="";
///#pragma location = "EMAC_DMA_RAM"
///static char out_SDRR[JSON_BUFFER_SIZE]="";
#pragma location = "EMAC_DMA_RAM"
static char out_AIN[JSON_BUFFER_SIZE]="";
#pragma location = "EMAC_DMA_RAM"
static char out_STAT[JSON_BUFFER_SIZE]="";

static unsigned int  offset1 = 0;
typedef enum
{
  SD_CMD_OK = 0,
  SD_CMD_TIMEOUT,
  SD_CMD_ERROR
} SD_CMD_t;


SD_CMD_t SD_COMP(char* sd_WARD);

#define MODEM_BUFFER_SIZE 2048 + 1
/*
#define MODEM_ENABLE_TXD_PIN() { PINSEL4 = (PINSEL4 & ~0x00000003) | 0x00000002; }
#define MODEM_ENABLE_RXD_PIN() { PINSEL4 = (PINSEL4 & ~0x0000000C) | 0x00000008; }
#define MODEM_ENABLE_CTS_PIN() { PINSEL4 = (PINSEL4 & ~0x00000030) | 0x00000020; }
#define MODEM_ENABLE_RTS_PIN() { PINSEL4 = (PINSEL4 & ~0x0000C000) | 0x00008000; }
#define MODEM_SET_RTS()        {FIO2DIR |= 0x00000080; FIO2SET =  0x00000080;}
#define MODEM_CLR_RTS()        {FIO2DIR |= 0x00000080; FIO2CLR =  0x00000080;}
#define MODEM_SET_DTR()        {FIO2DIR |= 0x00000020; FIO2CLR =  0x00000020;}
#define MODEM_CLR_DTR()        {FIO2DIR |= 0x00000020; FIO2CLR =  0x00000020;}
#define led_yellow_off()  (FIO1SET_bit.P1_15 = 1)
*/

#define GammaCryptEn    1

#define NOtyp      0
#define RCPUtyp    1
#define RMODtyp    2
#define RNETtyp    3
#define RDINtyp    4
#define RAINtyp    5
#define ROUTtyp    6
#define RSEBtyp    7
#define RCORtyp    8


#define GPRS      0
#define ETHERNET  1




/* Initialize modem with any baud: 0(autobaud), 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200 */
void modem_init(unsigned int baud);
void DelayS (unsigned long a) ;
/* Turn modem on */
void modem_on();
void CORRECT_CONFIG(int ckr);
/* Turn modem off */
void modem_off();
  void SET_TCN(void);
/* Turn on internal loopback mode */
void modem_loopbackon();
  
/* Turn off internal loopback mode */
void modem_loopbackoff();

/* Get number of available bytes in buffer */
int  modem_available();
#define Write 0
#define Read 1
int get_UPX();
int GET_GSM_READY();
int SET_diXR();
int SET_diXR1();
int CSQ_RESS(int CSQ_GS);
int gsm_init_status();
void set_dout(int DFQ1,int DFQ2);

void set_aout(int DFG1,int DFG2);
void SET_TSUP_GS(int SU);
void SET_TSSP_GS(int SSP);
void CSH(void);
void CSC(void);
void CSM(void);
void CSG(void);
void SETTINGS(int Config_SD);
/* Read data from modem buffer. Returns number of bytes read */
int  modem_read(char* dest, int length);

/* Send a single character using polling */
void modem_putc(char c);

void modem_debug(char *str);

/* Send string using polling */
 void modem_puts(char *str);

/* Send buffer using polling */
void modem_putn(char *str, int len);

/* Flush modem input buffer */
void modem_flush();
int SET_GSM_UR(int ASX);
#ifdef __cplusplus
}
#endif

#endif